// pages/rank_list/rank_list.js
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    portrait: '',
    rownum: 0,
    user_name: '',
    rankList: [],
    selectedIndex: 1,
  },

  //获取当前的排行信息
  getCurrentRank: function () {

  },

  initGodList: function () {
    var options = {
      list_id: 'rankList',
      url: '/api/index.php?app=account&act=guru_rank'
    }
    this.setData({
      rankOptions: options,
    })
  },

  initStarsList: function () {
    var options = {
      list_id: 'rankList',
      url: '/api/index.php?app=account&act=new_star_rank'
    }
    this.setData({
      rankOptions: options,
    })
  },

  //获取更多数据
  loadMoreData: function (e) {
    this.setData({
      rankList: e.detail.data,
    })
  },

  //获取当前排行的个人数据
  getRankInfo: function (type) {
    var url
    if (type == 0) {
      url = '/api/index.php?app=account&act=guru_rank'
    } else {
      url = '/api/index.php?app=account&act=new_star_rank'
    }
    app.Ajax({
      url: url,
      method: 'POST',
      data: {
        cur_page: 1,
      },
      success: (res) => {
        this.setData({
          rownum: res.user.rownum,
          portrait: res.user.portrait,
          user_name: res.user.user_name,
        })
      }
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var scrollListHeight = app.globalData.pageHeight;
    this.setData({
      scrollListHeight: scrollListHeight
    })
    this.initStarsList();
  },

  onShow: function (options) {
    this.getRankInfo(this.data.selectedIndex);
  },

  /**
   * 榜单切换
   */
  changeNavbar: function (e) {
    var index = e.currentTarget.dataset.index;
    if (index != this.data.selectedIndex) {
      if (index == 1) {
        this.initStarsList();
        this.getRankInfo(index);
      } else {
        this.getRankInfo(index);
        this.initGodList();
      }
      this.setData({
        selectedIndex: index
      })
    }
  }
})